﻿<MCard Width="500" Style="margin: 0 auto">
    <MVirtualScroll Height="250" Items="_items"
                    Id="infinite-scroll-virtual">
        <ItemContent>
            <div>
                <MListItem>
                    <MListItemContent>
                        <MListItemTitle>@context</MListItemTitle>
                    </MListItemContent>
                </MListItem>
                <MDivider />
            </div>
        </ItemContent>
        <FooterContent>
            <MInfiniteScroll OnLoad="@OnLoad"
                             Parent="#infinite-scroll-virtual">
            </MInfiniteScroll>
        </FooterContent>
    </MVirtualScroll>
</MCard>


@code {

    private MVirtualScroll<string>? _vs;
    private int _count;
    private bool _mockError;
    private List<string> _items = new();

    private async Task OnLoad(InfiniteScrollLoadEventArgs args)
    {
        var append = await MockRequest();

        args.Status = append.Count == 0 ? InfiniteScrollLoadStatus.Empty : InfiniteScrollLoadStatus.Ok;

        _items.AddRange(append);
    }

    private async Task<List<string>> MockRequest()
    {
        if (_count >= 3)
        {
            return new List<string>();
        }

        await Task.Delay(2000);

        if (_count == 1 && !_mockError)
        {
            _mockError = true;
            throw new TimeoutException("Mock error");
        }

        _count++;

        return Enumerable.Range(_items.Count, 10).Select(n => n.ToString()).ToList();
    }

}
